import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data () {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        brand: undefined,
        title: undefined,
        img: []
      },
      brandOptions: [],
      rules: {
        title: [
          {
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          },
        ],
        content: [
          {
            required: true,
            message: '请输入内容',
            trigger: 'blur'
          },
        ],
      },
    }
  },
  computed: {},
  watch: {},
  created () {
  },
  mounted () {
    this.$store.dispatch("base/getDictionaryData", { sort: "Brand" }).then((res) => {
      this.brandOptions = res;
    });
  },
  methods: {
    goBack () {
      this.$emit('refresh')
    },
    init (id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields();
        if (this.dataForm.id) {
          request({
            url: '/api/campus/banner/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data;
          })
        }
      })
    },
    dataFormSubmit () {
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.id) {
            request({
              url: `/api/campus/banner`,
              method: 'post',
              data: this.dataForm,
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false,
                    this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/campus/banner/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    },
  }
}
